﻿$(document).ready(function () {

    var today = new Date();

    $(".from.datepicker.search").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        maxDate: today,
        onClose: function (selectedDate) {
            $(".to.datepicker.search").datepicker("option", "minDate", selectedDate);
        }
    });
    $(".to.datepicker.search").datepicker({
        //        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        showOn: "button",
        maxDate: today,
        onClose: function (selectedDate) {
            $(".from.datepicker.search").datepicker("option", "maxDate", selectedDate);
        }
    });

    var currentPage = 1;
    var pageSize = 5;

    // add multiple select / deselect functionality
    $('#divBookingList').on('click', '#allChoice', function () {
        $('.choice').prop('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $('#divBookingList').on('click', '.choice', function () {

        if ($(".choice").length == $(".choice:checked").length) {
            $("#allChoice").prop("checked", true);
        } else {
            $("#allChoice").prop("checked", false);
        }
    });

    var openDialog = function () {

        var booking = bookingImplement.getBooking(parseInt($(this).closest('tr').attr('id')));

        loadBookingData(booking);

        $('#approval-dialog').dialog('open');

    };

    $('#divBookingList').on('click', '.button-view', openDialog);

    $('#btnDelete').click(function () {
        var selectedItems = [];
        $(".choice:checked").each(function () {
            selectedItems.push(parseInt($(this).closest('tr').attr('id')));
        });
        var result = bookingImplement.deleteBookings(selectedItems, status);

        if (result) {
            loadBookings(currentPage);
        }
    });

    function loadBookings(pageNum, isPaged) {
        var status = $('#ddlStatus option:selected').val();
        var bookingObjects = bookingImplement.getBookingList(status, pageNum,
            $('#keyword').val(),
            $('#fromDate').val(),
            $('#toDate').val());

        if (bookingObjects !== null) {
            $('#divBookingList').setTemplateURL("/Templates/bookingList.htm");
            $('#divBookingList').processTemplate(bookingObjects);

            if (!isPaged) {
                paginator(bookingObjects.RowCount, bookingObjects.PageSize);
            }
        }

        currentPage = pageNum;
    }

    function paginator(rowCount, pageSize) {
        $('#pager').smartpaginator({
            totalrecords: rowCount,
            recordsperpage: pageSize,
            length: 5,
            next: 'Next',
            prev: 'Prev',
            first: 'First',
            last: 'Last',
            theme: 'blue',
            controlsalways: true,
            onchange: function (newPage) {
                loadBookings(newPage, true);
                currentPage = newPage;
                $('#r').html('Page # ' + newPage);
            }
        });
    }

    $('#btnFind').click(function () { loadBookings(1); });

    loadBookings(1);
});